require.config({
	paths : {
		"jquery" : "../jquery",
		"cookie" : "../jquery.cookie"
	}
})
require(["jquery","cookie"],function($,cookie){
	init();
	appendDom();
	if($.cookie("login")){
		$("#tools #userlogin a").html("用户:" + $.cookie("login"));
		$("#tools #userrequire").css("display","none")
	}else{
		$("#tools #userlogin a").html("[登录]");
		$("#tools #userrequire").css("display","block")
	}
	$("#dynamic h2 input").click(function(){
		if($(this).is(':checked')){
			$("#dynamic h3 input").prop("checked",true);
			$("#dynamic h4 input").prop("checked",true);
		}else{
			$("#dynamic h3 input").removeAttr("checked");
			$("#dynamic h4 input").removeAttr("checked");
		}
	})
	$("#dynamic h4 input").click(function(){
		if($(this).is(':checked')){
			$("#dynamic h3 input").prop("checked",true);
			$("#dynamic h2 input").prop("checked",true);
		}else{
			$("#dynamic h3 input").removeAttr("checked",false);
			$("#dynamic h2 input").removeAttr("checked",false);
		}
	})
	$("#dynamic h4 #clearempty").click(function(){
		var obj = $.cookie();
		for(var i in obj){
			if(i.slice(0,7) == "product"){
				$.removeCookie(i, { path: '/' });
			}
		}
		window.location.reload();
	})
	//删除选中
	$("#dynamic h4 #delchose").click(function(){
		var $group = $("#dynamic h3 .chosepro");
		console.log($group.length);
		for(var i = 0;i <  $group.length;i ++){
			if($($group[i]).is(":checked")){
				$.removeCookie("product" + $($group[i]).siblings(".del").attr("id"), { path: '/' });
			}
		}
		window.location.reload();
	})
	$("#dynamic h3 .chosepro").click(function(){
		var a = 1;
		$("#dynamic h3 .chosepro").each(function(value,item){
			if($(item).is(":checked")){
				a *= 1;
				console.log("1");
			}else{
				a *= 0;
				console.log("0");
			}
		})
		if(a){
			$("#dynamic h2 input").prop("checked",true);
			$("#dynamic h4 input").prop("checked",true);
		}else{
			$("#dynamic h2 input").removeAttr("checked");
			$("#dynamic h4 input").removeAttr("checked");
		}
	})
	function summation(){
		var sum = 0;
		for(var i = 0;i < $("#dynamic h3 .sum").length; i ++){
			$($("#dynamic h3 .sum")[i]).html("¥" + (parseFloat($($("#dynamic h3 .sum")[i]).siblings(".price").html().replace("¥","")) *  ~~$($("#dynamic h3 .sum")[i]).siblings(".txt").val()).toFixed(2));
			sum += parseFloat($($("#dynamic h3 .sum")[i]).html().replace("¥",""));
		}
		$("#dynamic h4 #sumprice").html("¥" + sum.toFixed(2));
	}
	summation();
	function appendDom(){
		var arr = getCookie();
		arr.forEach(function(value){
			var mycookie = JSON.parse(value);
			$product = $('<h3><input class="chosepro" type="checkbox" /><img src="" alt="" /><span class="inform"></span><span class="price"></span><span class="sub">-</span><input class="txt" value="1" type="text" /><span class="add">+</span><span class="sum"></span><span></span><span class="del">删除</span></h3>')
			$("hr").before($product);
			$product.find(".txt").val(mycookie.num);
			$product.find("img").attr("src",mycookie.img);
			$product.find(".del").attr("id",mycookie.id);
			$product.find(".inform").html(mycookie.infor);
			$product.find(".price").html(mycookie.price);
			$product.find(".add").click(function(){
				$(this).siblings(".txt").val(~~$(this).siblings(".txt").val() + 1);
				var id = $(this).siblings(".del").attr("id");
				var obj = JSON.parse($.cookie("product" + id));
				obj.num = $(this).siblings(".txt").val();
				$.cookie("product" + id,JSON.stringify(obj),{ expires: 7, path: '/' } );
				summation();
			})
			$product.find(".sub").click(function(){
				if(~~$(this).siblings(".txt").val() > 1){
					$(this).siblings(".txt").val(~~$(this).siblings(".txt").val() - 1)
				}else{
					$(this).siblings(".txt").val("1");
				}
				var id = $(this).siblings(".del").attr("id");
				var obj = JSON.parse($.cookie("product" + id));
				obj.num = $(this).siblings(".txt").val();
				$.cookie("product" + id,JSON.stringify(obj),{ expires: 7, path: '/' } );
				summation();
			})
			$product.find(".txt").blur(function(){
				if(!(/^\d+$/.test($(this).val()))){
					$(this).val("1");
				}
				var id = $(this).siblings(".del").attr("id");
				var obj = JSON.parse($.cookie("product" + id));
				obj.num = $(this).val();
				$.cookie("product" + id,JSON.stringify(obj),{ expires: 7, path: '/' } );
				summation();
			})
			$product.find(".del").click(function(){
				$.removeCookie("product" + $(this).attr("id"), { path: '/' });
				window.location.reload();
			})
		})
	}
	$("#citylist ul li").each(function(){
		$(this).hover(function(){
			var $index = $(this).index();
			$("#citylist ul li").removeClass("showlist");
			$(this).addClass("showlist");
			$("#citylist ul .list1").css("display","none");
			$("#citylist ul .list1").eq($index).css("display","block");
		})	
	})
	function init(){
		var arrProduct = getCookie();
		if(arrProduct.length){
			$("#content #empty").css("display","none");
			$("#content #dynamic").css("display","block");
			$("#content h1").css("display","block");
			$("#content").css({"background":"#fff","borderTop":"none"});
		}else{
			$("#content #empty").css("display","block");
			$("#content #dynamic").css("display","none");
			$("#content h1").css("display","none");
			$("#content").css("background","#fff url(../img/none-bg.jpg) no-repeat 436px 150px");
		}
	}
	function getCookie(){
		var arrProduct = [];
		for(var i in $.cookie()){
			if(i.slice(0,7) == "product"){
				arrProduct.push($.cookie()[i]);
			}
		}
		return arrProduct;
	}
})
